import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

import { setToken, getToken,removeToken } from "@/utils/storage";
import { userInfoAPI } from "@/api";

const store = new Vuex.Store({
  state: {
    tokenObj: getToken() || {},
    userInfo: {},
  },
  mutations: {
    saveToken(state, payload) {
      state.tokenObj = payload;
      setToken(payload);
    },
    logout(state){
    state.tokenObj = {}
    state.userInfo ={}
    removeToken()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload;
    },
  },
  actions: {
    async reqUserInfo(context) {
      if (!context.state.userInfo.name) {
        let res = await userInfoAPI();
        context.commit("setUserInfo", res.data.data);
      }
    },
  },
});
export default store;
